﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace TetrisClient
{
    /// <summary>
    /// This class represents the user and contains the details of it. In addition, it is responsible
    /// to check if the user is registered or not on the data base and verify that the user has typed
    /// his password correctly.
    /// </summary>
    public class User : INotifyPropertyChanged
    {
        private string login;
        private string password;
        private string confirmPassword;

        /// <summary>
        /// Gets or sets the login.
        /// </summary>
        /// <value>
        /// The login.
        /// </value>
        /// <exception cref="System.ComponentModel.DataAnnotations.ValidationException">A login is required</exception>
        public string Login
        {
            get { return login; }
            set
            {
                    if (string.IsNullOrEmpty(value))
                    {
                        login = null;
                        throw new ValidationException("A login is required");
                    }
                    else
                    {
                        login = value;
                    }
                }
        }

        /// <summary>
        /// Gets or sets the password.
        /// </summary>
        /// <value>
        /// The password.
        /// </value>
        /// <exception cref="System.ComponentModel.DataAnnotations.ValidationException">A password is required</exception>
        public string Password
        {
            get { return password; }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    password = null;
                    throw new ValidationException("A password is required");
                }
                else
                {
                    password = value;
                }
            }
        }

        /// <summary>
        /// Gets or sets the confirm password field. It also compares it with the password
        /// field. If they do not match or if it the confirm password field blank, an exception is thrown
        /// </summary>
        /// <value>
        /// The confirm password.
        /// </value>
        /// <exception cref="System.ComponentModel.DataAnnotations.ValidationException">
        /// A password is required
        /// or
        /// Passwords mismatch
        /// </exception>
        public string ConfirmPassword
        {
            get { return confirmPassword; }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    confirmPassword = null;
                    throw new ValidationException("A password is required");
                }
                else if (value != password)
                {
                    confirmPassword = null;
                    throw new ValidationException("Passwords mismatch");
                }
                else
                {
                    confirmPassword = value;
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged = delegate { };
    }
}
